package m202401.d13;

import java.util.LinkedList;
import java.util.List;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/1/13 16:26
 */
//129. 求根节点到叶节点数字之和
public class Q129 {


    public int sumNumbers(TreeNode root) {
        return travel(root, 0);
    }

    public int travel(TreeNode node, int deep) {
        if (node == null) {
            return 0;
        }
        deep = deep * 10 + node.val;
        if (node.left == null && node.right == null) {
            return deep;
        }
        return travel(node.left, deep) + travel(node.right, deep);
    }


    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
